from flask import Blueprint,render_template,jsonify,request
from database import SevenDayWeather,WeatherStatistics,Wind
from sqlalchemy import or_
#from form import WindForm,WeatherStatisticsForm
from operation import WeatherStatisticsOperation,WindOperation
from json import dump

bp=Blueprint('weather',__name__,url_prefix='/weather')

#天气统计
weather_statistics_city1,weather_statistics_city2,weather_statistics_city3='北京','上海','深圳'
weather_statistics_weather1,weather_statistics_weather2,weather_statistics_weather3='Sunny','Cloudy','Showers'
#风速风向分析
wind_city='广州'

@bp.route('/')
def weather():
    return render_template('weather.html')

@bp.route('/SevenDayWeather')
def update_SevenDayWeather():
    weather_datas=list(SevenDayWeather.query.order_by(SevenDayWeather.joint_time.desc()).limit(7))
    weather_data_list = []
    for weather_data in reversed(weather_datas):
        weather_data_dict = {'date': weather_data.date, 'week': weather_data.week,'weather': weather_data.weather,
                             'high_temperature': weather_data.high_temperature,'low_temperature': weather_data.low_temperature}
        weather_data_list.append(weather_data_dict)
    print(weather_data_list)
    with open('json/SevenDayWeather.json', 'w',encoding="utf-8") as file:
        dump(weather_data_list, file, indent=2,ensure_ascii=False)  # 将数据写入到名为"data.json"的文件中 indent:缩进

    return jsonify(weather_data_list)

@bp.route('/WeatherStatisticsCity')
def set_WeatherStatisticsCity():
    global weather_statistics_city1,weather_statistics_city2,weather_statistics_city3
    print(weather_statistics_city1,weather_statistics_city2,weather_statistics_city3)
    if request.method=='GET':
        #weather_statistics_city1 = '广州'
        return jsonify({'weather_statistics_city1':weather_statistics_city1,'weather_statistics_city2':weather_statistics_city2,
                        'weather_statistics_city3':weather_statistics_city3,'code':200})
    else:
        city1 = request.json.get("weather_statistics_city1")
        city2 = request.json.get("weather_statistics_city2")
        city3 = request.json.get("weather_statistics_city3")
        flag=WeatherStatisticsOperation(city1,city2,city3).check_citys()
        if flag:#form.validate()
            weather_statistics_city1 = city1
            weather_statistics_city2 = city2
            weather_statistics_city3 = city3
            return jsonify({'weather_statistics_city1': weather_statistics_city1,
                            'weather_statistics_city2': weather_statistics_city2,
                            'weather_statistics_city3': weather_statistics_city3, 'code': 200})
        else:
            #print(form.errors)
            return jsonify({'weather_statistics_city1': weather_statistics_city1,
                            'weather_statistics_city2': weather_statistics_city2,
                            'weather_statistics_city3': weather_statistics_city3, 'code': 400})

@bp.route('/WeatherStatistics')
def get_WeatherStatistics():
    #weather_datas=list(WeatherStatistics.query.order_by(WeatherStatistics.joint_time.desc()).limit(10))
    weather_datas=list(WeatherStatistics.query.filter(or_(WeatherStatistics.city==weather_statistics_city1,WeatherStatistics.city==weather_statistics_city2,WeatherStatistics.city==weather_statistics_city3),
                or_(WeatherStatistics.weather==weather_statistics_weather1,WeatherStatistics.weather==weather_statistics_weather2,WeatherStatistics.weather==weather_statistics_weather3)).order_by(WeatherStatistics.joint_time.desc()).all())
    weather_data_list = []
    for weather_data in weather_datas:#reversed()
        weather_data_dict = {'city': weather_data.city,'weather': weather_data.weather,'day': weather_data.day}
        weather_data_list.append(weather_data_dict)
    print(weather_data_list)
    with open('json/WeatherStatistics.json', 'w',encoding="utf-8") as file:
        dump(weather_data_list, file, indent=2,ensure_ascii=False)  # 将数据写入到名为"data.json"的文件中 indent:缩进

    return jsonify(weather_data_list)

@bp.route('/WindCity',methods=['GET','POST'])
def set_WindCity():
    global wind_city
    print(wind_city)
    if request.method=='GET':
        # wind_city='汕头'
        # print(wind_city)
        return jsonify({'wind_city':wind_city,'code':200})
    else:
        #form =WindForm(request.form)
        city = request.json.get("wind_city")
        flag=WindOperation.check_city(city)
        if flag:#form.validate()
            wind_city=city
            return jsonify({'wind_city': wind_city, 'code': 200})
        else:
            #print(form.errors)
            return jsonify({'wind_city':wind_city,'code':400})

@bp.route('/wind')
def update_wind():
    weather_datas=list(Wind.query.filter(Wind.city==wind_city).order_by(Wind.time.desc()).limit(7))
    weather_data_list = []
    for weather_data in reversed(weather_datas):#
        weather_data_dict = {'city':weather_data.city,'time': str(weather_data.time),'windSpeed': weather_data.speed,'R':weather_data.rad}
        weather_data_list.append(weather_data_dict)
    print(weather_data_list)
    with open('json/Wind.json', 'w',encoding="utf-8") as file:
        dump(weather_data_list, file, indent=2,ensure_ascii=False)  # 将数据写入到名为"data.json"的文件中 indent:缩进

    return jsonify(weather_data_list)
